﻿using System;
using NPOI.SS.UserModel;

namespace Soul.Excel
{
    public class ExcelDataColumn
    {
        public string Name { get; }

        public string DisplayName { get; }

        public int? Width { get; set; } = 8;

        public string DataFormat { get; set; }

        public object DefaultValue { get; set; }

        public bool WrapText { get; set; } = false;

        public ExcelAlignment? Alignment { get; set; }
        
        public bool IsHidden { get;  set; }
        
        public ExcelDataColumn(string name)
        {
            Name = name;
            DisplayName = name;
        }

        public ExcelDataColumn(string name,string displayName)
        {
            Name = name;
            DisplayName = displayName;
        }

        private ICellStyle _style;

        internal ICellStyle GetStyle(IWorkbook book, int fontHeight)
        {
            if (_style == null)
            {
                var font = book.CreateFont();
                font.IsBold = false;
                font.FontHeight = fontHeight;
                var style = book.CreateCellStyle();
                var dataFormat = book.CreateDataFormat();
                if (!string.IsNullOrEmpty(DataFormat))
                {
                    style.DataFormat = dataFormat.GetFormat(DataFormat);
                }
                if (Alignment == ExcelAlignment.Left)
                {
                    style.Alignment = HorizontalAlignment.Left;
                    style.VerticalAlignment = VerticalAlignment.Center;
                }
                else if (Alignment == ExcelAlignment.Right)
                {
                    style.Alignment = HorizontalAlignment.Right;
                    style.VerticalAlignment = VerticalAlignment.Center;
                }
                else
                {
                    style.Alignment = HorizontalAlignment.Center;
                    style.VerticalAlignment = VerticalAlignment.Center;
                }
                style.SetFont(font);
                style.WrapText = WrapText;
                DefaultExcelStyles.InitStyle(style);
                _style = style;
            }
            return _style;
        }
    }
}
